﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace ExpressionUtil.Node.Common
{
    /// <summary>
    /// 执行环境（主要负责维护执行过程中得状态数据，如：返回值传递等）
    /// </summary>
    [Serializable]
    internal class ExecuteContext : ISerializable
    {
        /// <summary>
        /// 临时数字运算结果
        /// </summary>
        private decimal globalTempNum;

        /// <summary>
        /// 临时布尔类型运算结果
        /// </summary>
        private bool globalTempBool;

        /// <summary>
        /// 数字变量集合
        /// </summary>
        private Dictionary<string, decimal> decimalVariables;

        /// <summary>
        /// 布尔类型变量集合
        /// </summary>
        private Dictionary<string, bool> boolVariables;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ExecuteContext()
        {
            this.decimalVariables = new Dictionary<string, decimal>();
            this.boolVariables = new Dictionary<string, bool>();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="info">要填充数据的 System.Runtime.Serialization.SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public ExecuteContext(SerializationInfo info, StreamingContext context)
        {
            this.globalTempNum = info.GetDecimal("globalTempNum");
            this.globalTempBool = info.GetBoolean("globalTempBool");
            this.decimalVariables = info.GetValue("decimalVariables", typeof(Dictionary<string, decimal>)) as Dictionary<string, decimal>;
            this.boolVariables = info.GetValue("boolVariables", typeof(Dictionary<string, bool>)) as Dictionary<string, bool>;
        }

        /// <summary>
        /// 获取或设置临时数字运算结果
        /// </summary>
        public decimal GlobalTempNum
        {
            get
            {
                return this.globalTempNum;
            }
            set
            {
                this.globalTempNum = value;
            }
        }

        /// <summary>
        /// 获取或设置临时布尔类型运算结果
        /// </summary>
        public bool GlobalTempBool
        {
            get
            {
                return this.globalTempBool;
            }
            set
            {
                this.globalTempBool = value;
            }
        }

        /// <summary>
        /// 获取或设置数字变量集合
        /// </summary>
        public Dictionary<string, decimal> DecimalVariables
        {
            get
            {
                return this.decimalVariables;
            }
            set
            {
                this.decimalVariables = value;
            }
        }

        /// <summary>
        /// 获取或设置布尔类型变量集合
        /// </summary>
        public Dictionary<string, bool> BoolVariables
        {
            get
            {
                return this.boolVariables;
            }
            set
            {
                this.boolVariables = value;
            }
        }

        #region ISerializable 成员

        /// <summary>
        /// 获取对象数据
        /// </summary>
        /// <param name="info">要填充数据的 System.Runtime.Serialization.SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("globalTempNum", this.globalTempNum);
            info.AddValue("globalTempBool", this.globalTempBool);
            info.AddValue("decimalVariables", this.decimalVariables, typeof(Dictionary<string, decimal>));
            info.AddValue("boolVariables", this.boolVariables, typeof(Dictionary<string, bool>));
        }

        #endregion
    }
}
